package com.jswlw.controller;

import com.jswlw.common.Constants;
import com.jswlw.common.ResponseStatus;
import com.jswlw.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by wu on 2017-12-11.
 */
@Slf4j
@ControllerAdvice
public class BaseController {

    public Logger logger = LoggerFactory.getLogger(this.getClass());


    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    /**
     * 获取成功的返回值
     *
     * @return
     */
    public <T> ModelMap successResult(T data) {
        ModelMap result = new ModelMap();

        result.put(Constants.CODE, ResponseStatus.SUCCESS.getCode());
        result.put(Constants.MSG, ResponseStatus.SUCCESS.getMsg());
        result.put(Constants.DATA, data);

        return result;
    }

    /**
     * 获取成功的返回值
     *
     * @return
     */
    public <T> ModelMap successResult(T rows, int total) {
        ModelMap result = new ModelMap();

        result.put(Constants.CODE, ResponseStatus.SUCCESS.getCode());
        result.put(Constants.MSG, ResponseStatus.SUCCESS.getMsg());
        result.put(Constants.ROWS, rows);
        result.put(Constants.TOTAL, total);

        return result;
    }

    public <T> ModelMap pageResult(T rows, Long total) {
        ModelMap result = new ModelMap();

        result.put(Constants.CODE, ResponseStatus.SUCCESS.getCode());
        result.put(Constants.MSG, ResponseStatus.SUCCESS.getMsg());
        result.put(Constants.ROWS, rows);
        result.put(Constants.TOTAL, total);
        result.put(Constants.CODE, ResponseStatus.SUCCESS.getCode());
        return result;
    }

    /**
     * 获取成功的返回值
     *
     * @return
     */
    public <T> ModelMap successResult(T rows, int total, Map extra) {
        ModelMap result = new ModelMap();

        result.put(Constants.CODE, ResponseStatus.SUCCESS.getCode());
        result.put(Constants.MSG, ResponseStatus.SUCCESS.getMsg());
        result.put(Constants.ROWS, rows);
        result.put(Constants.TOTAL, total);
        result.put(Constants.CODE, ResponseStatus.SUCCESS.getCode());
        result.putAll(extra);
        return result;
    }

    /**
     * 失败返回值通过该类获取对应的返回值结构
     *
     * @param message
     * @return
     */
    public ModelMap failResult(String message) {
        ModelMap result = new ModelMap();
        result.put(Constants.CODE, ResponseStatus.BUSINESS_ERROR.getCode());
        result.put(Constants.MSG, message);
        return result;
    }

    public Map parseParams(HttpServletRequest request) {
        Map params = new HashMap<>();
        Map<String, String[]> requestMap = request.getParameterMap();
        Iterator iterator = requestMap.keySet().iterator();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            String[] objs = requestMap.get(key);
            if(StringUtils.isNotEmpty(objs[0])){
                params.put(key, objs[0]);
            }

        }
        return params;
    }

    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if(ip.equals("127.0.0.1")){
                //根据网卡取本机配置的IP
                InetAddress inet=null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ip= inet.getHostAddress();
            }
        }
        // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if(ip != null && ip.length() > 15){
            if(ip.indexOf(",")>0){
                ip = ip.substring(0,ip.indexOf(","));
            }
        }
        return ip;
    }

    public static void main(String[] args) {
        System.out.println(Integer.valueOf("1000").byteValue());
    }
}
